package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ExpertNewVO;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.ExpertListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/3 15:03
 * 修改备注：
 * ================================================
 */
public class ChooseExpertsListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;

    private List<ExpertNewVO> expertVOList = new ArrayList<>();
    private String maxTypeCode = "";
    private String minTypeCode = "";
    private String keyWord = "";
    private List<String> picturePath;

    private Map<String, Object> map;
    private String pAddress; // 个人地址
    private String typeName; // 作物种类Name

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_choose_experts);
        setTitle("选择咨询专家");
    }

    @Override
    public void initMember() {

        map = (Map<String, Object>) getIntent().getSerializableExtra("map");
        keyWord = (String) map.get("keyWord");
        picturePath = (List<String>) map.get("picturePath");
        pAddress = (String) map.get("address");
        maxTypeCode = (String) map.get("maxTypeCode");
        minTypeCode = (String) map.get("minTypeCode");
        typeName = (String) map.get("typeName");

        initRecyclerView();
        BusinessExpert.expertSelect(this, String.valueOf(page), String.valueOf(limit), maxTypeCode, minTypeCode, mHandler);
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ExpertListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                final ExpertNewVO expertVO = (ExpertNewVO) adapter.getAllData().get(position);
                String title;


                if (StringUtil.isStrEmpty(pAddress) && StringUtil.isStrEmpty(typeName)) { // 只发问题
                    keyWord = String.format("您好，我遇到的问题是：%n%s", keyWord);
                } else if (!StringUtil.isStrEmpty(pAddress) && StringUtil.isStrEmpty(typeName)) { // 只发问题和地址
                    keyWord = String.format("您好，我是来自%s的用户，我遇到的问题是：%n%s", pAddress, keyWord);
                } else if (!StringUtil.isStrEmpty(pAddress) && !StringUtil.isStrEmpty(typeName)) { // 问题 地址 作物分类
                    keyWord = String.format("您好，我是来自%s的用户，作物分类为%s，我遇到的问题是：%n%s", pAddress, typeName, keyWord);
                } else { // 所需个人信息完整
                    keyWord = String.format("您好，我是来自%s的用户，作物分类为%s，我遇到的问题是：%n%s", pAddress, typeName, keyWord);
                }

                if (picturePath != null && picturePath.size() > 0) {
                    String path = "";
                    for (int i = 0; i < picturePath.size(); i++) {
                        if (i < picturePath.size() - 1)
                            path += picturePath.get(i) + ",";
                        else
                            path += picturePath.get(i);
                    }
                    title = StringUtil.jointString(expertVO.getExpertName(), "&", keyWord, "&", path);
                } else
                    title = StringUtil.jointString(expertVO.getExpertName(), "&", keyWord);



                // 跳转后关闭其他页面
                finish();
                ActivityTaskManager.getInstance().closeActivity(HelpAndSupportActivity.class);
                ActivityTaskManager.getInstance().closeActivity(ConsultIssueV2Activity.class);
                ActivityTaskManager.getInstance().closeActivity(IssueDetailActivity.class);
            }
        });
    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_EXPERT_SELECT_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_EXPERT_SELECT_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {
        expertVOList = FastJsonUtil.getListBean(object.toString(), "body", "experts", ExpertNewVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }


        adapter.addAll(expertVOList);

        if (expertVOList != null) {
            if (expertVOList.size() < limit) {
                adapter.stopMore();
            }
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessExpert.expertSelect(this, String.valueOf(page), String.valueOf(limit), maxTypeCode, minTypeCode, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (expertVOList != null && expertVOList.size() >= limit) {
            page++;
            BusinessExpert.expertSelect(this, String.valueOf(page), String.valueOf(limit), maxTypeCode, minTypeCode, mHandler);
        }
    }
}
